//
//  StatusCell.swift
//  sinaweibowj
//
//  Created by Macintosh on 15/12/19.
//  Copyright © 2015年 mactoinsh. All rights reserved.
//

import UIKit
import SnapKit

class StatusCell: UITableViewCell {
    
    //定义视图的底部约束属性
    var bottomContraints:Constraint?
    
    //定义微博模型属性
    var status:Status?{
        didSet{
            //给顶部视图设置模型数据
            topView.status = status
            
            //卸载约束
            self.bottomContraints?.uninstall()
            
            if let retweetedStatus = status?.retweeted_status{
                //一定是转发微博
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomContraints = make.top.equalTo(retweetedView.snp_bottom).constraint
                })
                //设置转发微博的模型数据
                retweetedView.retweetedStatus = retweetedStatus
                //转发微博显示
                retweetedView.hidden = false
            }
            else {
                //一定是原创微博
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomContraints = make.top.equalTo(topView.snp_bottom).constraint
                })
                //转发微博隐藏
                retweetedView.hidden = true
            }
        }
    }
    
    
    //入口
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        //设置cell的选中样式
        selectionStyle = .None
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //自定义cell
    private func setupUI (){
        //添加子视图
        contentView.addSubview(topView)
        contentView.addSubview(bottomView)
        contentView.addSubview(retweetedView)
        
        //设置约束
        //顶部视图的约束
        topView.snp_makeConstraints { (make) -> Void in
            make.left.right.top.equalTo(contentView)
            //设置高度便于测试
            //make.height.equalTo(400)
            // make.bottom.equalTo(bottomView.snp_bottom)
        }
        
        //转发微博视图的约束
        retweetedView.snp_makeConstraints { (make) -> Void in
            make.left.right.equalTo(contentView)
            make.top.equalTo(topView.snp_bottom)
            //便于前期调试
            //make.height.equalTo(50)
            
        }
        
        //底部视图的约束
        bottomView.snp_makeConstraints { (make) -> Void in
            make.left.right.equalTo(contentView)
            self.bottomContraints = make.top.equalTo(retweetedView.snp_bottom).constraint
            make.height.equalTo(40)
        }
        
        
        
        
        //非常关键的一点 给contentView添加约束
        contentView.snp_makeConstraints { (make) -> Void in
            
            make.top.left.right.equalTo(self)
            make.bottom.equalTo(bottomView.snp_bottom)
        }
        
    }
    
    //懒加载
    //顶部视图
    private lazy var topView:StatusCellTopView = StatusCellTopView()
    //转发微博
    private lazy var retweetedView:StatusRetweetedView = StatusRetweetedView()
    //底部视图
    private lazy var bottomView:StatusCellBottomView = StatusCellBottomView()
    
}
